<?php
/**
 * 字典管理
 * @author 479468212@qq.com
 */

namespace app\admin\controller;

use app\common\model\CommonDataDict as dictModel;

class DataDict extends Base {

    /**
     * @OA\Get(
     *   path="/admin/DataDict/index",
     *   tags={"数据字典相关"},
     *   summary="字典列表",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(name="type",in="query",required=true,description="检索类型",@OA\Schema( type="string" )),
     *   @OA\Parameter(name="keywords",in="query",required=true,description="关键词",@OA\Schema( type="string" )),
     *   @OA\Parameter(ref="#/components/parameters/size"),
     *   @OA\Parameter(ref="#/components/parameters/page"),
     *   @OA\Response(
     *     response="200",
     *     description="返回JSON", 
     *     @OA\MediaType(
     *      mediaType="application/json",
     *      @OA\Schema(
     *         @OA\Property(property="code", type="number",description="状态码"),
     *         @OA\Property(property="msg", type="string",description="提示消息"),
     *      )
     *    )
     *   ),
     *   @OA\Response(response="500", description="服务器错误"),
     *   security={{"App-Login-Token": ""}}
     * )
     */
    public function index() {
        $limit   = $this->request->get('size', config('easyApi.ADMIN_LIST_DEFAULT'));
        $type    = $this->request->get('type');
        $keywords= $this->request->get('keywords');
        $obj = new dictModel();
        if($type == 'name'){
            $obj = $obj->where(['name'=>$keywords]);
        }
        if($type == 'title'){
            $obj = $obj->where(['title'=>$keywords]);
        }
        $list    = $obj->order('id desc')->paginate($limit, false)->toArray();

        $this->success('获取成功',[
            'list'  => $list['data'],
            'count' => $list['total']
        ]);
    }

    /**
     * @OA\Get(
     *   path="/admin/DataDict/getDict",
     *   tags={"数据字典相关"},
     *   summary="获取最后一个字典",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Response(
     *     response="200",
     *     description="返回JSON",
     *     @OA\MediaType(
     *      mediaType="application/json",
     *      @OA\Schema(
     *         @OA\Property(property="code", type="number",description="状态码"),
     *         @OA\Property(property="msg", type="string",description="提示消息"),
     *      )
     *    )
     *   ),
     *   @OA\Response(response="500", description="服务器错误"),
     *   security={{"App-Login-Token": ""}}
     * )
     */
    public function getDict() {
        $obj = new dictModel();
        $find = $obj->order('id desc')->find();
        $this->success('获取成功',$find);
    }

    /**
     * @OA\Post(
     *   path="/admin/DataDict/add",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   tags={"数据字典相关"},
     *   summary="字典添加",
     *   @OA\RequestBody(
     *      @OA\MediaType(
     *         mediaType="application/json",
     *         @OA\Schema(
     *            @OA\Property(property="title", type="string", description="标题"),
     *            @OA\Property(property="name", type="string", description="取值名称"),
     *            @OA\Property(property="value", type="string", description="值"),
     *            @OA\Property(property="remark", type="string", description="备注")
     *         )
     *       )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="OK",
     *         @OA\JsonContent( type="array", @OA\Items(ref="#/components/schemas/response" ) )
     *     ),
     *     @OA\Response(response="500", description="服务器错误"),
     *     security={{"App-Login-Token": ""}}
     * )
     */
    public function add() {
        $postData = $this->request->post();
        if(empty($postData['name'])){
            $this->error("缺少取值名称");
        }
        if(empty($postData['title'])){
            $this->error("缺少标题");
        }
        if(empty($postData['show_name'])){
            $this->error("缺少显示名称");
        }
        if(strlen($postData['value']) <= 0){
            $this->error("缺少值");
        }
        $res = \think\facade\Db::name('common_data_dict')->cache('dataDict:'.$postData['name'])->strict(false)->insert($postData);
        if ($res === false) {
            $this->error();
        } else {
            $this->success();
        }
    }

    /**
     * @OA\Put(
     *   path="/admin/DataDict/edit",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   tags={"数据字典相关"},
     *   summary="字典编辑",
     *   @OA\RequestBody(
     *      @OA\MediaType(
     *         mediaType="application/json",
     *         @OA\Schema(
     *            @OA\Property(property="title", type="string", description="标题"),
     *            @OA\Property(property="name", type="string", description="取值名称"),
     *            @OA\Property(property="value", type="string", description="值"),
     *            @OA\Property(property="remark", type="string", description="备注")
     *         )
     *       )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="OK",
     *         @OA\JsonContent( type="array", @OA\Items(ref="#/components/schemas/response" ) )
     *     ),
     *     @OA\Response(response="500", description="服务器错误"),
     *     security={{"App-Login-Token": ""}}
     * )
     */
    public function edit() {
        $postData = $this->request->param();
        if(empty($postData['name'])){
            $this->error("缺少取值名称");
        }
        if(empty($postData['title'])){
            $this->error("缺少标题");
        }
        if(empty($postData['show_name'])){
            $this->error("缺少显示名称");
        }
        if(strlen($postData['value']) <= 0){
            $this->error("缺少值");
        }
        $res = \think\facade\Db::name('common_data_dict')->cache('dataDict:'.$postData['name'])->strict(false)->update($postData);
        if ($res === false) {
            $this->error();
        } else {
            $this->success();
        }
    }

    /**
     * @OA\Put(
     *     path="/admin/DataDict/changeStatus/{id}",
     *     tags={"数据字典相关"},
     *     summary="字典状态",
     *     @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *     @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *     @OA\Parameter(name="id",in="path",required=true,description="记录ID",@OA\Schema( type="integer", format="int32", minimum=1.0 )),
     *     @OA\RequestBody(
     *         @OA\MediaType(
     *             mediaType="application/json",
     *             @OA\Schema(
     *                 @OA\Property( property="status", type="string", description="状态。0废弃，1正常" ),
     *                 example={"status": 1}
     *             )
     *         )
     *     ),
     *     @OA\Response(
     *         response=200,
     *         description="OK"
     *     ),
     *     security={{"App-Login-Token": ""}}
     * )
     */
    public function changeStatus($id) {
        $status = $this->request->param('status');
        $find = dictModel::find($id);
        $res = dictModel::cache('dataDict:'.$find['name'])->update([
            'id'   => $id,
            'status' => $status
        ]);
        if ($res === false) {
            $this->error();
        } else {
            $this->success();
        }
    }

    /**
     * @OA\Delete(path="/admin/DataDict/delete/{id}",
     *   tags={"客户端相关"},
     *   summary="删除版本号",
     *   @OA\Parameter(ref="#/components/parameters/AppClientVersion"),
     *   @OA\Parameter(ref="#/components/parameters/AppDeviceType"),
     *   @OA\Response(
     *     response=200,
     *     description="返回Json",
     *     @OA\JsonContent( type="array", @OA\Items(ref="#/components/schemas/response" ) )
     *   ),
     *   @OA\Response(response=500, description="服务器错误"),
     *   security={{"App-Login-Token": ""}}
     * )
     */
    public function delete($id) {
        if (!$id) {
            $this->error('缺少必要参数');
        }
        $find = dictModel::find($id);
        $result = \think\facade\Db::name('common_data_dict')->cache('dataDict:'.$find['name'])->where('id','=',$id)->delete();
        if (!$result) {
            $this->error('删除失败');
        }
        $this->success();
    }

}
